var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    index: '',
    searchShowed: false,
    searchVal: "",
    selected: false,
    hasNoData:false
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName +"--常用路线" });
    this.loadData();
  },
  loadData() {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    util.getData("usuallyPath", {
      CO_ID: app.globalData.CO_ID,
      val: that.data.searchVal
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (res.data.success) {
        var _data = res.data.data;
        that.setData({
          pathData: _data
        })
      }else{
        that.setData({
          pathData: [],
          hasNoData:true
        })
      }
    });
  },
  onReady: function () {
    wx.hideToast()
  },
  showSearch: function () {
    this.setData({
      searchShowed: true
    });
  },
  hideSearch: function () {
    this.setData({
      searchVal: "",
      searchShowed: false
    });
    this.loadData();
  },
  clearSearch: function () {
    this.setData({
      searchVal: ""
    });
    this.loadData();
  },
  searchTyping: function (e) {
    var that = this;
    that.setData({
      searchVal: e.detail.value
    });
    that.loadData();
  },
  selectPath: function (e) {
    var that = this;
    var id = String(e.currentTarget.id);
    var pathData = this.data.pathData;
    for (var i = 0; i<pathData.length;i++){
      if (pathData[i].ID == id){
        wx.setStorage({
          key: "path_usually",
          data: pathData[i]
        });
        wx.redirectTo({
          url: '/pages/index/deliveryForm'
        })
      }
    }
  },
  copyPath: function (e) {
    wx.redirectTo({
      url: '/pages/index/usuallyPathNew?mode=new&id='+e.currentTarget.id+'&name='+e.currentTarget.dataset.name
    })
  },
  editPath: function (e) {
    wx.redirectTo({
      url: '/pages/index/usuallyPathNew?mode=edit&id='+e.currentTarget.id+'&name='+e.currentTarget.dataset.name
    })
  },
  addNewPath: function () {
    wx.redirectTo({
      url: '/pages/index/usuallyPathNew'
    })
  },
  deletePath: function (e) {
    var id = e.currentTarget.id;
    wx.showModal({
      title: '删除路线',
      content: '是否确认删除本路线',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '请稍候...',
          });
          util.putData("deletePath", {
            id: id
          }, function (res, error) {
            console.log(res); console.log(error)
            wx.hideLoading();
            if (error) {
              wx.showToast({
                title: res.data.data,
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
            if (res.data.success) {
              wx.showToast({
                title: "删除成功",
                icon: 'success',
                success: function () {
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '/pages/index/usuallyPath'
                    })
                  }, 3000);
                }
              });
            }
          });
        } else if (res.cancel) {
        }
      }
    })
  },
  cancelConfirmData: function () {
    wx.redirectTo({
      url: '/pages/index/deliveryForm'
    })
  }
});